<?php
/**
 * Description of Login
 *
 * @author pangpang
 */
class Login extends TrjhCommand implements TrjhCmdInterface {

    const USER_NOT_EXIST = 1001;
    const PHONE_ERROR = 1003;
    const PASSWORD_ERROR = 1004;

    public function run($param) {
        parent::run($param);

        $phone = $param->username;
        $password = $param->password;

        $isPhone = $this->app->valid->check('username', $phone);
        $isPassword = $this->app->valid->check('password', $password);

         
        if ($isPhone == FALSE) {
            $ret = $this->errorHandle($this::PHONE_ERROR);
            return $ret;
        }

        if ($isPassword == FALSE) {
            $ret = $this->errorHandle($this::PASSWORD_ERROR);
            return $ret;
        }

        //引入认证处理类
        $this->auth = new Auth($this->app->logger,$this->app->redis);

        //查询用户
        //引入用户模型类
        $usersModel = new Users_model($this->app);
        $userEntity = $usersModel->getByPhone($phone);

        $uid = $userEntity['id'];

        if ($userEntity == []) {
            $ret = $this->errorHandle($this::USER_NOT_EXIST);
            return $ret;
        }
        //密码校验
        //略

       
        //生成身份token
        $this->app->logger->info("生成身份token");
        $access_token = $this->auth->getToken($uid);
        $this->app->logger->info("生成身份token" . $access_token);
        
        //将身份认证写入Redis
        $this->auth->setAuth($access_token, $uid);
        $this->app->logger->info("将身份认证写入Redis队列");
        return $this->returnHandle(["token" => $access_token]);
    }

}
